<?php
defined('IN_TS') or die('Access Denied.');

switch($ts){
	case "":
		if(isset($_padmin)){
			header("Location: /index.php");
		}else{
			if(isset($_COOKIE['gym_admin'])){
				$adminuser=$_COOKIE['gym_admin'];
			}
			include template("login");
		}
		include template("login");
		break;

	case "do":
		if($_POST['token'] != $_SESSION['token']) {
			tsNotice('非法操作！');
		}

		$username = trim($_POST['username']);
		$pwd = trim($_POST['userpass']);

		if($username=='' || $pwd=='') mt_alert("所有输入项都不能为空!");

		$u=$new['home']->find('user',array('mobile'=>$username));
		if($u==null){
			setcookie('mt_login_num',$mt_login_num+1,time()+3600);
			$lc=9-$mt_login_num;
			mt_alert("管理名不存在！您还有{$lc}次登录机会");
		}
		if(md5($u['salt'].$pwd)!==$u['password']){
			unset($_SESSION['gymchina']);
			setcookie('mt_login_num',$mt_login_num+1,time()+3600);
			$lc=9-$mt_login_num;
			mt_alert("用户密码错误！您还有{$lc}次登录机会");
		}

		if($u['isv']==2){
			mt_alert('你已离职，不能登录本系统！');
		}

		if(R()->exists("gymchina:{$u['userid']}")){
			R()->delete("gymchina:{$u['userid']}");
		}
		R()->hMset("gymchina:{$u['userid']}",$u);
		setcookie('gymchina_userid',$u['userid'],time()+36000);
		setcookie('mt_login_num',0,time()-3600);

		$content='于'.date('Y-m-d H:i:s').'登录精中学院';
		aac('home')->adminlog($u['userid'],$u['username'],$u['cid'],$content);

		header("Location: /index.php");
		break;

		//退出
	case "out":
		R()->delete("gymchina:{$_padmin['userid']}");
		setcookie('gymchina_userid',0,time()-3600);
		header("Location: index.php?ac=login");
		break;

	case "sendcode":
		$mb=trim($_POST['mb']);
		$ad=$new['home']->find('user',array('mobile'=>$mb),'isadmin');
		if($ad==null){
			echo 2;
		}else{
			if($ad['isadmin']==0){
				echo 3;
			}else{
				echo 1;
			}
		}
		break;

}